
// 悬浮导航菜单
$(function() {
	var $elem = $(".fixed-by-scroll");
	if ($elem.length == 0) return;
	var findPanel = function(element, selector) {
		if (!selector) return $(document.body);
		if (selector == "this") return element;
		if (selector == "parent") return element.parent();
		if (selector == "root") return $(document);
		if (selector == "body") return $(document.body);
		return element.closest(selector);
	};
	var fixed = false;
	var $elem = $(".fixed-by-scroll");
	var o = $elem.zoptions();
	var $panel = findPanel($elem, o.panel);
	var $targets = $panel.find(o.target);
	var min = $elem.offset().top;
	var currIndex = 0;
	$(window).scroll(function() {
		var scrollTop = $(window).scrollTop();
		if (scrollTop < min) {
			if (fixed) { $elem.removeClass("fixed"); }
			fixed = false;
		} else {
			if (!fixed) { $elem.addClass("fixed"); }
			fixed = true;
		}
		var newIndex = 0;
		for (var i = $targets.length - 1; i >= 0; i--) {
			var p = $targets.eq(i).offset().top;
			if (scrollTop > p - 100) {
				newIndex = i;
				break;
			}
		}
		if (currIndex != newIndex) {
			var items = $elem.find(".item");
			items.eq(currIndex).removeClass("active");
			items.eq(newIndex).addClass("active");
			currIndex = newIndex;
		}
	});
	
	$elem.on("click", ".item", function() {
		var index = $elem.find(".item").index(this);
		var scrollTop = $targets.eq(index).offset().top - 50;
		$(window).scrollTop(scrollTop); // jQuery针对scrollTop作了处理, 调用的是window.scrollTo(x, y);
	});
});

